package com.palfund.threadpool;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

/**
 * Thread实现了Runnable接口
 * <p>
 * yield()礼让一次,此后执行由cup分配
 * <p>
 * 线程只能执行一次start()多次start会报错(java.lang.IllegalThreadStateException: Thread already started)
 * <p>
 * 新建:new Thread()
 * 就绪:执行了start()--->可运行状态(runnable)
 * 运行:被cpu执行--->running
 * 阻塞:因为一些阻塞事件,导致线程暂时不能执行,sleep(),wait(),join().....
 * 死亡:线程结束(run()方法执行完毕)
 * T1线程中调用T2线程的join()方法(t2.join())-->t2线程结束后,t1线程再执行...
 */
public class ThreadActivity extends AppCompatActivity {
    private TestThread mTestThread;
    private WaitThread mWaitThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread);
        setTitle("Thread");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, 1, "test interrupt");
        menu.add(Menu.NONE, 2, 1, " ");
        menu.add(Menu.NONE, 3, 1, "test isAlive");
        menu.add(Menu.NONE, 4, 1, "test start");
        menu.add(Menu.NONE, 5, 1, "test stop");
        menu.add(Menu.NONE, 6, 1, "wait start");
        menu.add(Menu.NONE, 7, 1, "wait isAlive");
        menu.add(Menu.NONE, 8, 1, " ");
        menu.add(Menu.NONE, 9, 1, "try catch");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                //打断
                mTestThread.interrupt();
                Log.i("ThreadActivity", "---onOptionsItemSelected()--->" + "test打断");
                break;
            case 2:
                break;
            case 3:
                //是否存活
                Log.i("ThreadActivity", "---onOptionsItemSelected()--->isAlive   " + mTestThread
                        .isAlive());
                break;
            case 4:
                //开始
                mTestThread = new TestThread();
                mTestThread.start();
                break;
            case 5:
                //无法暂停
                mTestThread.stop();
                break;
            case 6:
                //start wait thread
                mWaitThread = new WaitThread();
                mWaitThread.start();
                break;
            case 7:
                //wait Thread 是否存活
                Log.i("ThreadActivity", "---onOptionsItemSelected()--->" + mWaitThread.isAlive());
                break;
            case 8:
                break;
            case 9:
                //try catch
                Log.i("ThreadActivity", "---onOptionsItemSelected()--->" + tryCatch());
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * Test Thread
     */
    class TestThread extends Thread {
        @Override
        public void run() {
            super.run();
            Log.i("TestThread", "---run()--->" + "开始执行");
            //interrupt();
            //Log.i("TestThread", "---run()--->" + isInterrupted());
            try {
                sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //Log.i("TestThread", "---run()--->" + isInterrupted());
            Log.i("TestThread", "---run()--->" + "执行完毕");
        }
    }

    class WaitThread extends Thread {
        @Override
        public void run() {
            super.run();
            Log.i("WaitThread", "---run()--->" + "开始执行");
            //interrupt();
            //Log.i("WaitThread", "---run()--->" + isInterrupted());
            synchronized (WaitThread.class) {
                try {
                    WaitThread.class.wait(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            //Log.i("WaitThread", "---run()--->" + isInterrupted());
            Log.i("WaitThread", "---run()--->" + "结束执行");
        }
    }

    /**
     * 没有try catch捕获异常,发生异常处程序就崩溃不在继续执行;
     * try catch捕获异常,try中产生异常处之后的代码不再执行,catch中、finally中以及try catch以外代码按顺序执行
     * catch中有return则finally继续执行,try catch以外代码不再执行;finally中有return,则catch中return不执行
     */
    private int tryCatch() {
        Log.i("ThreadActivity", "---tryCatch()--->" + "开始");
        try {
            int i = 1 / 0;
            Log.i("ThreadActivity", "---tryCatch()--->" + "产生异常");
        } catch (ArithmeticException e) {
            e.printStackTrace();
            Log.i("ThreadActivity", "---tryCatch()--->" + "catch捕获异常");
            return 0;
        } finally {
            Log.i("ThreadActivity", "---tryCatch()--->" + "finally");
            return 1;
        }
        //Log.i("ThreadActivity", "---tryCatch()--->" + "结束");
    }
}
/**
 * wait(),sleep()的区别
 * A:出处不同:
 * wait-->Object类
 * sleep-->Thread类
 * B:醒来的方式不同
 * wait-->时间到自然醒、notify,notifyAll、interrupt
 * sleep-->时间到自然醒、interrupt
 * C:对锁定资源的释放不同
 * wait-->释放了锁资源
 * sleep-->没有释放锁资源
 * volatile
 * 多线程的内存模型:main memory(主存)、working memory(线程栈),在处理数据时,线程会把值从主存load到本地栈,
 * 完成操作后再save回去(volatile关键词的作用:每次针对该变量的操作都激发一次load and save)。
 * <p>
 * interrupt会抛出异常java.lang.InterruptedException
 * interrupt是一个打断标记,调用后代表该线程需要打断。先执行interrupt再执行sleep、wait则sleep、wait直接被打断;
 * 正在执行sleep、wait的调用interrupt则sleep、wait被打断.
 * <p>
 * Log.i("TestThread", "---run()--->" + "开始执行");
 * interrupt();
 * Log.i("TestThread", "---run()--->" + isInterrupted());
 * sleep(5000);
 * Log.i("TestThread", "---run()--->" + isInterrupted());
 * Log.i("TestThread", "---run()--->" + "执行完毕");
 * log日志如下:sleep被打断
 * TestThread: ---run()--->开始执行
 * TestThread: ---run()--->true
 * TestThread: ---run()--->false
 * TestThread: ---run()--->执行完毕
 * <p>
 * Log.i("TestThread", "---run()--->" + "开始执行");
 * Log.i("TestThread", "---run()--->" + isInterrupted());
 * sleep(5000);
 * interrupt();
 * Log.i("TestThread", "---run()--->" + isInterrupted());
 * Log.i("TestThread", "---run()--->" + "执行完毕");
 * TestThread: ---run()--->开始执行
 * TestThread: ---run()--->false
 * TestThread: ---run()--->true
 * TestThread: ---run()--->执行完毕
 */